<?php
    $getParam = $_GET;

    if(empty($getParam)){
        exit('非法访问，退出登录');
    }

    switch ($getParam["model"]){
        case 'register':
            $nowData = $_POST;
            //第一步，链接数据库
            $link = @mysqli_connect("localhost","root","root","messageBoard","3306");
            //第二步，判断链接
            if(mysqli_connect_errno()){
                exit("链接失败，原因：".mysqli_connect_error());
            }
            //第三步，设置字符集。
            mysqli_set_charset($link,"utf8");
            //第四步，判断字符集是否设置成功；
            if(mysqli_errno($link)){
                exit("字符集设置失败，原因：".mysqli_error($link));
            }
            $sql = "select * from `userList` where `userName` = '".$nowData["userName"]."'";

            //第六步，执行sql；
            $result = mysqli_query($link,$sql);
            $xxx = mysqli_fetch_row($result);
            if(!empty($xxx)){
                echo json_encode(["code"=>2,"msg"=>"账户名已经存在"]);
            }else {
                //第五步，拼接sql;`person`
                $fieldStr = '';
                $valueStr = '';
                foreach ($nowData as $key => $value) {
                    $fieldStr .= "`{$key}`,";
                    $valueStr .= "'{$value}',";
                }

                $fieldStr = substr($fieldStr, 0, -1);
                $valueStr = substr($valueStr, 0, -1);

                $sql = sprintf("insert into `userList`(%s) values(%s)", $fieldStr, $valueStr);
                //第六步，执行sql；
                $result = mysqli_query($link, $sql);
                //第七步上，查询；
                $arr = [];
                /*while($row=mysqli_fetch_assoc($result)){
                    $arr[]=$row;
                }*/
                //第七步下，操作；
                if ($result) {
                    echo json_encode(["code" => 1, "msg" => "增加成功"]);
                } else {
                    echo json_encode(["code" => 2, "msg" => "增加失败"]);
                }
                //第八步，关闭链接，释放资源。
                mysqli_close($link);
            }
        break;

        case 'login' :
            $nowData = $_POST;
            //第一步，链接数据库
            $link = @mysqli_connect("localhost","root","root","messageBoard","3306");
            //第二步，判断链接
            if(mysqli_connect_errno()){
                exit("链接失败，原因：".mysqli_connect_error());
            }
            //第三步，设置字符集。
            mysqli_set_charset($link,"utf8");
            //第四步，判断字符集是否设置成功；
            if(mysqli_errno($link)){
                exit("字符集设置失败，原因：".mysqli_error($link));
            }
            $sql = "select * from `userList` where `userName` = '".$nowData["userName"]."' and `password` = '".$nowData["password"]."'";

            //第六步，执行sql；
            $result = mysqli_query($link,$sql);
            $xxx = mysqli_fetch_row($result);

            if(empty($xxx)){
                echo json_encode(["code"=>2,"msg"=>"账户或密码错误"]);
            }else {
                echo json_encode(["code" => 1, "msg" => "登录成功"]);
            }
            //第八步，关闭链接，释放资源。
            mysqli_close($link);
        break;

        case 'articleShow' :
            //$nowData = $_POST;
            //第一步，链接数据库
            $link = @mysqli_connect("localhost","root","root","messageBoard","3306");
            //第二步，判断链接
            if(mysqli_connect_errno()){
                exit("链接失败，原因：".mysqli_connect_error());
            }
            //第三步，设置字符集。
            mysqli_set_charset($link,"utf8");
            //第四步，判断字符集是否设置成功；
            if(mysqli_errno($link)){
                exit("字符集设置失败，原因：".mysqli_error($link));
            }
            $sql = "select * from `article`";

            //第六步，执行sql；
            $result = mysqli_query($link,$sql);
            $arr = [];
            while($row=mysqli_fetch_assoc($result)){
                $arr[]=$row;
            }
            if(empty($arr)){
                echo json_encode(["code"=>2,"msg"=>"查询错误"]);
            }else {
                echo json_encode(["code" => 1, "msg" => "查询成功",'data'=>$arr]);
            }
            //第八步，关闭链接，释放资源。
            mysqli_close($link);
        break;
        default:
            echo '郑州开发中';
        break;
    }

?>

